<?php
//---------------------------------------------------------
//               Buddha Bless, No Bug !
//         User: wuxuehai Date: 2018/7/31 Time: 下午3:40
//---------------------------------------------------------

namespace common\models;


use common\behaviors\ModelCacheBehavior;
use yii\behaviors\TimestampBehavior;
use yii\caching\ChainedDependency;
use yii\caching\DbDependency;
use yii\caching\ExpressionDependency;
use yii\db\ActiveQuery;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;

class BaseModel extends ActiveRecord
{
    const STATUS_INACTIVE = 0;
    const STATUS_ACTIVE = 1;

    const CACHE_ON = true;
    const CACHE_DURATION = 600;
    const CACHE_PREFIX = 'lastModifyTime_';

    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::className(),
            ],
            [
                'class' => ModelCacheBehavior::className(),
            ]
        ];
    }

    /**
     * {@inheritdoc}
     * @return ActiveQuery the newly created [[ActiveQuery]] instance.
     */
    public static function find()
    {
        $activeQuery = parent::find(); // TODO: Change the autogenerated stub

        if (static::CACHE_ON) {
            $activeQuery->cache(static::CACHE_DURATION, static::getCacheDependency());
        }

        return $activeQuery;
    }

    /**
     * 缓存依赖
     * @return ExpressionDependency
     */
    public static function getCacheDependency()
    {
        $key = static::getAutoCacheDependencyKey();
        return new ExpressionDependency(
            ['expression' => '\Yii::$app->cache->get(' . $key . ')']
        );
    }

    /**
     * @return array
     */
    public static function getStatusOptions()
    {
        return [
            self::STATUS_INACTIVE => '关闭',
            self::STATUS_ACTIVE => '启用',
        ];
    }

    /**
     * @param $status
     * @return mixed
     */
    public static function getStatusName($status)
    {
        $item = static::getStatusOptions();
        return ArrayHelper::getValue($item, $status, '无');
    }


    /**
     * 获取总条数
     * @param array $where
     * @return int|string
     */
    public static function count(array $where = [])
    {
        return static::find()->where($where)->count();
    }

    /**
     * 获取option列表
     * @param array $where
     * @param string $from
     * @param string $to
     * @return array
     */
    public static function getListForOptions($where = ['status' => self::STATUS_ACTIVE], $from = 'id', $to = 'name')
    {
        $list = static::findAll($where);
        return ArrayHelper::map($list, $from, $to);
    }

    /**
     * 根据ID获取字段值
     * @param $id
     * @param $field
     * @return mixed|string
     */
    public static function getFieldById($id, $field)
    {
        $info = static::find()->where(['id' => $id])->one();
        return ArrayHelper::getValue($info, $field);
    }

    public static function getAutoCacheDependencyKey()
    {
        return static::CACHE_PREFIX . \Yii::$app->db->schema->getRawTableName(static::tableName());
    }
}
